<?php

namespace app\model;

use app\BaseModel;
use think\model\relation\BelongsTo;
use think\model\relation\HasMany;
use app\model\not_del\UserModel as NotDelUserModel;

/**
 * 任务
 */
class TaskModel extends BaseModel
{
    protected $name = 'task';

    /**
     * 新增数据验证
     * @return array
     */
    public function addValidate(): array
    {
        return [
            'name|任务名称' =>  'require',
        ];
    }

    /**
     * 关联单位
     * @return BelongsTo
     */
    public function unitName(): BelongsTo
    {
        return $this->belongsTo(UnitModel::class,'unit_id')->bind([
            'unit_name'=>'name'
        ]);
    }

    /**
     * 关联用户
     * @return BelongsTo
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(NotDelUserModel::class,'uid')->bind(['user_name'=>'name','unit_name']);
    }

    /**
     * 关联用户名称
     * @return BelongsTo
     */
    public function userName(): BelongsTo
    {
        return $this->belongsTo(NotDelUserModel::class,'uid')->bind(['user_name'=>'name']);
    }

    /**
     * 关联用户名称和角色
     * @return BelongsTo
     */
    public function userNameRoleName(): BelongsTo
    {
        return $this->belongsTo(NotDelUserModel::class,'uid')->bind(['user_name'=>'name','role_name']);
    }

    /**
     * 关联答题角色为用户的数据
     * @return HasMany
     */
    public function AnswerRoleUser(): HasMany
    {
        return $this->hasMany('TaskUserResultModel','task_id')->where('answer_role','=',1);
    }

    /**
     * 关联答题角色为家长的数据
     * @return HasMany
     */
    public function AnswerRoleParent(): HasMany
    {
        return $this->hasMany('TaskUserResultModel','task_id')->where('answer_role','=',2);
    }

    public function taskRatingScale(): HasMany
    {
        return $this->hasMany(TaskRatingScaleModel::class,'task_id');
    }

    public function taskContinuousEvaluation(): HasMany
    {
        return $this->hasMany(TaskContinuousEvaluationModel::class,'task_id');
    }
}